<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>产品配置</el-breadcrumb-item>
        <el-breadcrumb-item>多语言文本配置</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
        @row-update="rowUpdate"
        @row-save="rowSave"
        @row-del="rowDel">
        <template slot="dynamic_able" slot-scope="scope" >
          <i class="el-icon-error" style="color: red" v-if="scope.row.dynamic_able==0"></i>
          <i class="el-icon-success" style="color: green" v-else></i>
        </template>
        <template slot="config_complete" slot-scope="scope" >
          <i class="el-icon-error" style="color: red" v-if="scope.row.config_complete==0"></i>
          <i class="el-icon-success" style="color: green" v-else></i>
        </template>
        <template slot="last" slot-scope="scope">
          {{ scope.row.edit_time }}
          <el-tag>{{ scope.row.edit_admin_name }}</el-tag>
        </template>
        <template slot-scope="scope" slot="menu">
            <el-button
              icon="el-icon-edit"
              type="text"
              size="small"
              @click.stop="rowEdit(scope.row)">
                <font>编辑</font>
            </el-button>
            <el-button
              icon="el-icon-delete"
              type="text"
              size="small"
              v-if="scope.row.use_area==1"
              @click.stop="rowDel(scope.row)">
                <font>删除</font>
            </el-button>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import { appConfigDefaultTxt as baseURL,selectVersion} from "@/api/cat/baseUrls";
export default {
  data() {
    return {
      page: {
        total: 0,
        pageSize: 10
      },
      type: true,
      data: [],
      params: {},
      form: {},
      default: {
        orderByList: "config_complete&ASC,create_time&ASC"
      },
      option: {
        border: true,
        selection: true,
        border: true,
        delBtn: false,
        editBtn:false,
        align: "center",
        headerAlign: "center",
        column: [
          {
            label: "说明",
            prop: "txt",
            type: "input",
            search: true,
            placeholder: "文字说明,可输入中文,不可重复",
            rules: [
              {
                required: true,
                message: "说明必须填写",
              },
            ],
          },
          {
            label: "使用区域",
            prop: "use_area",
            type: "select",
            search: true,
            addDisplay: true,
            editDisplay: true,
            addDisabled: false,
            editDisabled: true,
            span:13,
            placeholder: "请选择使用区域",
            dicData:[{label:'后台配置',value:1},{label:'系统配置',value:2},{label:'响应回复',value:3}],
            rules: [
              {
                required: true,
                message: "使用区域必须选择",
              },
            ],
          },
          {
            label: "动态文本",
            prop: "dynamic_able",
            type: "select",
            search: true,
            slot: true,
            span:13,
            placeholder: "是否为动态文本",
            dicData:[{label:'否',value:0},{label:'是',value:1}],
            rules: [
              {
                required: true,
                message: "是否为动态文本必须选择",
              },
            ],
          },
          {
            label: "配置完整",
            prop: "config_complete",
            type: "select",
            search: true,
            addDisplay: false,
            editDisplay: false,
            addDisabled: false,
            editDisabled: false,
            slot: true,
            span:13,
            dicData:[{label:'否',value:0},{label:'是',value:1}]
          },
          {
            label: "最后操作信息",
            prop: "last",
            type: "input",
            addDisplay: false,
            editDisplay: false,
            addDisabled: false,
            editDisabled: false,
            readonly: true,
            slot: true
          },
          {
            label: "英语文本",
            prop: "en_txt",
            type: "textarea",
            placeholder: "英语文本",
            hide:true
          },
          {
            label: "芬兰语文本",
            prop: "fn_txt",
            type: "textarea",
            placeholder: "芬兰语文本",
            hide: true
          },
        ],
      },
    };
  },
  methods: {
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        `${baseURL.pageAndParam}`,
        { ...this.params, ...this.default },
        this.page,
        "POST"
      );
      this.page = response.page;
      this.data = response.data;
      this.loading = false;
    },
    /**保存 */
    async rowSave(row, done, loading) {
      delete row["id"];
      row.create_time = new Date();
      row.consume_type = 1;
      let userInfo = this.$store.state.user.userInfo;
      row.edit_admin_name = userInfo.username;
      row.edit_admin_id = userInfo.id;
      row.edit_time = new Date();
      let data = await common(`${baseURL.save}`, row, null, "POST");
      if (data == null) {
        return;
      }
      this.getList();
      done();
    },
    /**修改 */
    async rowUpdate(row, index, done, loading) {
      let userInfo = this.$store.state.user.userInfo;
      row.edit_admin_name = userInfo.username;
      row.edit_admin_id = userInfo.id;
      row.edit_time = new Date();
      let data = await common(`${baseURL.update}`, row, null, "POST");
      if (data == null) {
        return;
      }

      this.getList();
      done();
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**删除 */
    rowDel(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        type: "warning",
      })
        .then(async () => {
          let response = await common(
            `${baseURL.delete}`,
            { id: row.id },
            null,
            "GET"
          );
        })
        .then(() => {
          this.getList();
        });
    },
    /**编辑 */
    rowEdit(row) {
      if(row.use_area == 1){
        this.option.column[0].editDisabled = false
      }else{
        this.option.column[0].editDisabled = true
      }
      this.$refs.crud.rowEdit(row,row.$index);
    },
    /**搜索 */
    searchChange(params, done) {
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    }
  },
};
</script>